<?php

// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------


namespace app\admin\controller;

use think\Controller;
use think\Db;

use app\admin\model\SysRoleModel;
use app\admin\model\SysMenuModel;

class Sysrole extends Base
{


    public function index()
    {
        $rolemodule=new SysRoleModel();

        $rolelist=$rolemodule->getAllList();

      
        foreach($rolelist as $key=>$vo){
            $rolelist[$key]['operate'] = $this->getRowButton($vo['id']);
        }

        $this->assign([
            'rolelist' => $rolelist
        ]);

        return $this->fetch();


        return $this->fetch();

    }



    
    public function del()
    {
        $rolemodel = new SysRoleModel();
        if(request()->isPost()){
            $id = input("post.id");
            $msg = $rolemodel->delById($id);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
        return $this->fetch();
    }



    
    public function edit()
    {
        $rolemodel = new SysRoleModel();

        if(request()->isPost()){
            $param = input("post.");

            foreach ($param as $key=>$value){
                if(is_array($value))
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
            
            $msg = $rolemodel->edit($param);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
      
        $id = input('param.id');
        $role=$rolemodel->getById($id);
  
        $menmodule=new SysMenuModel();
        $menulist=doListLeftMenu($menmodule->getListbyRoleId(0),$role['menulist']);
     
      
        $this->assign([
            'role' => $role,
            'menulist' => $menulist
       
         
        ]);
        return $this->fetch();
    }


    

    public function add()
    {
    

        if(request()->isPost()){
            $rolemodel = new SysRoleModel();
            $param = input("post.");
            $msg = $rolemodel->add($param);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
      

        return $this->fetch();
    }



    
    /**
     * 操作按钮
     * @param $id
     */
    private function getRowButton($id)
    {
        $buttonlist= " <a href='edit?id=".$id."' class='btn btn-outline-primary btn-sm mod'>编辑</a> "
        ." <button class='btn btn-outline-primary btn-sm' onclick='del(this,".$id.");' >删除</button> ";
       return $buttonlist;
    }

    /**********************************LAYUI 基础版本修改 开始***********************************/

    /**
     * 获取角色数据
     * @return mixed
     */
    public function ajaxGetSysRole(){

        $model = new SysRoleModel();
        if(request()->isAjax()){
            $param = request()->param();
            $result = $model->getSysRole($param);
            return $result;
        }
    }

    /**
     * <权限设置> 获取权限列表
     * @return mixed
     */
    public function ajaxGetAuthlist(){
        $model = new SysRoleModel();
        if(request()->isAjax()){
            $result = $model->getRoleList();
            return $result;
        }
    }
    /**
     * 保存角色数据
     * @return mixed
     */
    public function ajaxSaveRoleData(){
        $model = new SysRoleModel();
        if(request()->isAjax()){
            $param = input("post.");
            $result = $model->ajaxSaveData($param);
            return $result;
        }
    }
    
}
